package com.ruicar.afs.cloud.apply.admin.manage.entity;

import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableName;
import com.ruicar.afs.cloud.common.core.entity.BaseEntity;
import com.ruicar.afs.cloud.common.core.security.vo.RoleVo;
import com.ruicar.afs.cloud.common.modules.apply.enums.DataTypeEnum;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.springframework.beans.BeanUtils;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * <p>
 * 角色表
 * </p>
 *
 * @author Fzero
 * @since 2017-10-29
 */
@Data
@EqualsAndHashCode(callSuper = true)
@TableName("sys_role")
public class SysRole extends BaseEntity<SysRole> {

    private static final long serialVersionUID = 1L;
    @NotBlank(message = "角色名称 不能为空")
    private String roleName;

    @NotBlank(message = "角色标识 不能为空")
    private String roleCode;

    @NotNull(message = "数据权限类型 不能为空")
    private DataTypeEnum dsType;

    private String dsScope;

    private String tenantId;


    /**
     * 角色类型;新车角色、二手车角色
     */
    private String roleType;

    @TableField(exist = false)
    @ApiModelProperty(value = "拥有权限")
    private List<SysRolePermission> permissions;

    @TableField(exist = false)
    @ApiModelProperty(value = "拥有数据权限")
    private List<String> departments;

    public RoleVo transToRoleVo() {
        RoleVo roleVo = new RoleVo();
        BeanUtils.copyProperties(this, roleVo);
        roleVo.setRoleId(this.getId().toString());
        return roleVo;
    }

    @TableField(exist = false)
    private String[] roleTypeTemp;

    public String[] getRoleTypeTemp() {
        return roleType.split(",");
    }

    public void setRoleTypeTemp(String[] roleTypeTemp) {
        String s = "";
        for (String str : roleTypeTemp) {
            s += str + ",";
        }
        roleType = s.substring(0, s.length() - 1);
        this.roleTypeTemp = roleTypeTemp;
    }
}
